﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using Umbraco.AzureStorage;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public IEnumerable<string> GetFileList(string ftpServerIP, string ftpUserID, string ftpPassword, string ftpPath)
        {
            List<string> result = new List<string>();
            WebResponse response = null;
            StreamReader reader = null;
            try
            {
                FtpWebRequest reqFTP;
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + ftpPath));
               //wywalic hasla zapisane na sztywno i dodać dzielenie na domene i usera
                reqFTP.Credentials = new NetworkCredential("admin11", ftpPassword, "SGP");
                reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
                reqFTP.KeepAlive = true;
                reqFTP.UsePassive = false;
                response = reqFTP.GetResponse();
                reader = new StreamReader(response.GetResponseStream());
                string line = reader.ReadLine();
                List<string> list = new List<string>();
                while (line != null)
                {
                    list.Add(line);
                    line = reader.ReadLine();
                }
                foreach (string s in list)
                {
                    if (s.Contains("<DIR>"))
                    {
                        string dictionaryName = s.Split(' ')[s.Split(' ').Length - 1];
                        result.AddRange(GetFileList(ftpServerIP, ftpUserID, ftpPassword, ftpPath + "/" + dictionaryName));
                    }
                    else
                    {
                        string fileName = s.Split(' ')[s.Split(' ').Length - 1];
                        result.Add(fileName);
                    }              
                }
                return result;
            }
            catch (Exception)
            {
                if (reader != null)
                {
                    reader.Close();
                }
                if (response != null)
                {
                    response.Close();
                }
                throw;
            }
        }

        private MemoryStream DownloadFile(string file, string ftpServerIP, string ftpUserID, string ftpPassword, string ftpPath)
        {
            try
            {
                string uri = "ftp://" + ftpServerIP + "/" + file;
                Uri serverUri = new Uri(uri);
                if (serverUri.Scheme != Uri.UriSchemeFtp)
                {
                    return null;
                }
                FtpWebRequest reqFTP;
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + ftpPath + "/" + file));
                reqFTP.Credentials = new NetworkCredential("admin11", ftpPassword, "SGP");
                reqFTP.KeepAlive = false;
                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                reqFTP.UseBinary = true;
                reqFTP.Proxy = null;
                reqFTP.UsePassive = false;
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                Stream responseStream = response.GetResponseStream();
                MemoryStream memoryStream = new MemoryStream();
                int Length = 2048;
                Byte[] buffer = new Byte[Length];
                int bytesRead = responseStream.Read(buffer, 0, Length);
                while (bytesRead > 0)
                {
                    memoryStream.Write(buffer, 0, bytesRead);
                    bytesRead = responseStream.Read(buffer, 0, Length);
                }
                response.Close();
                return memoryStream;
             }
            catch (Exception ex)
            {
                throw;
            }
        }
        protected void UploadFile(string path, MemoryStream memoryStream)
        {
            BlobFileSystem bfs = new BlobFileSystem("");
            bfs.AddFile(path, memoryStream);
            memoryStream.Close();          
        }
       
        protected void btnUpload_Click(object sender, EventArgs e)
        {
           
            string ftpServerIP = txtFtpAdress.Text;
            string ftpUserID = txtFtpUser.Text;
            string ftpPassword = txtPassword.Text;
            string ftpPath = txtFtpPath.Text;
            IEnumerable<string> fileList = GetFileList(ftpServerIP, ftpUserID, ftpPassword, ftpPath);
            foreach (string s in fileList)
            {
                MemoryStream memoryStream = DownloadFile(s, ftpServerIP, ftpUserID, ftpPassword, ftpPath);
                UploadFile(ftpPath, memoryStream);
            }
        }
    }
}